package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list", name = "进入角色管理页面")
    public String findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Role> pageInfo = roleService.findPage(getCompanyId(), page, pageSize);
        request.setAttribute("page", pageInfo);
        return "system/role/role-list";
    }

    @RequestMapping(value = "/toAdd", name = "进入角色新增页面")
    public String toAdd() {

        return "system/role/role-add";
    }

    @RequestMapping(value = "/edit", name = "保存角色方法")
    public String edit(Role role) {

        //role的id为空就是新增,否则修改
        if (StringUtils.isEmpty(role.getId())) {
            //新增时需要
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            role.setCreateTime(new Date());

            //id赋值一个随机id
            role.setId(UUID.randomUUID().toString());
            roleService.save(role);
        } else {
            role.setUpdateTime(new Date());
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "进入修改角色页面")
    public String toUpdate(String id) {
        //根据id查询部门
        Role role = roleService.findById(id);
        //放到request域中,用作数据回显
        request.setAttribute("role", role);

        //修改页面和新增页面公用一套
        return "system/role/role-add";
    }

    @RequestMapping(value = "/delete", name = "删除角色方法")
    public String delete(String id) {
        roleService.deleteById(id);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule", name = "进入角色分配权限页面")
    public String roleModule(String roleid) {
        Role role = roleService.findById(roleid);
        request.setAttribute("role", role);
        return "system/role/role-module";
    }

    @RequestMapping(value = "/getZtreeNodes", name = "构造模块数据")
    @ResponseBody
    public List<Map> getZtreeNodes(String roleid) {
        //查询此角色拥有的菜单权限
        List<String> moduleIdList =  moduleService.findModulesByRoleId(roleid);

        //1.查询所有模块
       // List<Module> moduleList = moduleService.findAll();
        //查询此企业具有的模块
        List<Module> moduleList = moduleService.findModuleListByCompanyId(getCompanyId());

        //2.构造map集合
        List<Map> listMap = new ArrayList<>();
        Map map = null;

        //3.遍历所有模块
        for (Module module : moduleList) {
            //初始化map
            map = new HashMap();
            //添加数据
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());

            //判断moduleIdList集合中是否包含module.getId()
            if (moduleIdList.contains(module.getId())){
                map.put("checked",true);
            }

            //存入list集合
            listMap.add(map);
        }
        //4.返回输入给ajax调用者
        return listMap;
    }

    @RequestMapping(value = "/updateRoleModule",name = "实现分配权限")
    public String updateRoleModule(String roleid,String moduleIds){
        //实现分配权限
        roleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";
    }

}
